﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace PresentationLayer
{
    public partial class ViewDetailOfRequisition : System.Web.UI.Page
    {
        ApproveRequisitionController approveController = new ApproveRequisitionController();
        //String userName = "DeptHead1";
        int empID, reqId;

        protected void Page_Load(object sender, EventArgs e)
        {

            string reqID = Request.QueryString["reqID"];
            empID = Convert.ToInt32(Request.QueryString["empID"]);
            reqId = Convert.ToInt32(reqID);
            if (!IsPostBack)
            {
                gvReqDetail.DataSource = approveController.getReqDet(reqId);
                gvReqDetail.DataBind();

                lblEmpName.Text = approveController.getEmpNameByEmpID(empID);
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            String deptID = approveController.getDeptID(empID);
            int rowCount = gvReqDetail.Rows.Count;
            List<String> rdoStatus = new List<string>();
            List<String> reasons = new List<string>();

            foreach (GridViewRow r in gvReqDetail.Rows)
            {
                RadioButton rdoApp = (RadioButton)r.FindControl("rdoApprove");
                RadioButton rdoRej = (RadioButton)r.FindControl("rdoReject");
                TextBox txtReason = (TextBox)r.FindControl("txtReason");

                if (rdoApp.Checked)
                {
                    rdoStatus.Add("Approve");
                }
                else
                {
                    rdoStatus.Add("Reject");
                }

                reasons.Add(txtReason.Text);
            }
            approveController.saveToDeptReq(deptID, reqId, rdoStatus, reasons);
            approveController.SendNotificationToEmployee(empID, deptID);
            Response.Redirect("~/ApprovalSuccessful.aspx");

        }
    }
}